#include "BitWidget.h"
#include <QString>
#include <QTableWidgetItem>
#include <QHeaderView>

BitWidget::BitWidget(int num)
    :ulCurVal(0U)
{
    this->num = num;
    CustomUI();
}

BitWidget::~BitWidget()
{

}
/*
* @brief 创建界面
* @note
*/
void BitWidget::CustomUI()
{
    tablewidget = new QTableWidget(1,num);
    tablewidget->setStyleSheet("background-color: grey;");
    QStringList slHeaderNo;
    for(int i = 0; i < num ; i++)
    {
        QTableWidgetItem* tablewidgetitem = new QTableWidgetItem();
        tablewidgetitem->setText("0");
        tablewidget->setItem(0,i,tablewidgetitem);
        slHeaderNo <<  QString::number(i);
    }
    tablewidget->setHorizontalHeaderLabels(slHeaderNo);
    //tablewidget->setEditTriggers(QAbstractItemView::NoEditTriggers);                // 设置成表格不能修改
    tablewidget->setHorizontalScrollBarPolicy(Qt::ScrollBarAsNeeded);
    tablewidget->horizontalHeader()->setSectionsClickable(false);                   // 设置表头不可点击
    tablewidget->horizontalHeader()->setSectionResizeMode(QHeaderView::ResizeToContents);  // 根据内容改变列宽 ResizeToContents
    tablewidget->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
    tablewidget->setAutoScroll(true);
    tablewidget->setVerticalScrollBarPolicy( Qt::ScrollBarAlwaysOff);               // 垂直滑块禁止使用
    tablewidget->verticalHeader()->setVisible(false);

    connect(tablewidget,SIGNAL(cellChanged(int,int)),this,SLOT(catchcellChangedHandler(int,int)));

    setCentralWidget(tablewidget);
}
/*
* @brief
* @note
*/
void BitWidget::catchcellChangedHandler(int row,int col)
{
    if(0 == tablewidget->item(row,col)->text().toInt())
    {//0
        ulCurVal |= ( col << 0 );
    }
    else
    {//1
        tablewidget->item(row,col)->setText("1");
        ulCurVal |= ( col << 1 );
    }
}

